from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib.pyplot as plt


# 载入数据
data = np.genfromtxt("data.csv", delimiter=",")
x_data = data[:,0]
y_data = data[:,1]
plt.scatter(x_data,y_data)
plt.show()
print(x_data.shape)
print(x_data)
"""
这里需要注意
fit函数原型
  Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            Training data

        y : array-like of shape (n_samples,) or (n_samples, n_targets)
            Target values. Will be cast to X's dtype if necessary
            
            
由于fit函数需要一个两维的数据
data[:,0,np.newaxis]
这是将一个一维数组变成二维数组
"""

x_data = data[:,0,np.newaxis]
print(x_data.shape)
print(x_data)
y_data = data[:,1,np.newaxis]
# 创建并拟合模型
model = LinearRegression()
model.fit(x_data, y_data)
# model.save()
# model.predict(x_data)

# 画图
plt.plot(x_data, y_data, 'b.')
plt.plot(x_data, model.predict(x_data), 'r')
plt.show()